﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using starLiGHT.ParticleSystem.Common;
using starLiGHT.ParticleSystem.twoD;
using ParticleSystemRendererFactoryMap = System.Collections.Generic.Dictionary<string, starLiGHT.SceneManagement.IParticleSystemRendererFactory>;

#endregion

#region Version Stuff
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 1379                  $:  Revision of last commit *
// * $Author:: glatzemann         $:  Author of last commit   *
// * $Date:: 2010-09-06 18:59:54 #$:  Date of last commit     *
// ************************************************************
#endregion

namespace starLiGHT.SceneManagement
{
#if !XNA4
    public class ParticleSystemManager
    {
        #region Singleton
        private static ParticleSystemManager instance;

        public static ParticleSystemManager Instance
        {
            get
            {
                if (instance == null) instance = new ParticleSystemManager();
                return instance;
            }
        }

        private ParticleSystemManager()
        {
            mFactory = new ParticleSystemFactory();
            ////////TODO: Core.Instance.addMovableObjectFactory(mFactory);
        }

        #endregion

        private ParticleSystemRendererFactoryMap mRendererFactories;
        private ParticleSystemFactory mFactory;

        public void _initialise()
        {
            addRendererFactory(new RenderTargetRendererFactory());
#if !ZUNE
            addRendererFactory(new RenderTargetVectorLineRendererFactory());
#endif
            addRendererFactory(new BitmapLineRendererFactory());
            addRendererFactory(new MovableObjectRendererFactory());
            addRendererFactory(new PointSpriteRendererFactory());
            addRendererFactory(new VectorLineRendererFactory());
        }

        public ParticleSystem createSystemImpl(string name, int quota, string resourceGroup, GraphicsDevice graphics)
        {
            ParticleSystem sys = new ParticleSystem(name, resourceGroup, graphics);
            sys.ParticleQuota = quota;
            return sys;
        }

        public void addRendererFactory(IParticleSystemRendererFactory factory)
        {
            if (mRendererFactories == null)
            {
                mRendererFactories = new Dictionary<string, IParticleSystemRendererFactory>();
            }

            string name = factory.Type;
            mRendererFactories[name] = factory;
            Trace.WriteLine("Particle Renderer type '" + name + "' registered.");
        }

        public IRenderer _createRenderer(string rendererType, GraphicsDevice graphics)
        {
            if (!mRendererFactories.ContainsKey(rendererType))
            {
                throw new Exception("Cannot find requested renderer type.");
            }

            return mRendererFactories[rendererType].createInstance(rendererType, graphics);
        }

        public void _destroyRenderer(IRenderer renderer)
        {
            if (!mRendererFactories.ContainsKey(renderer.Type))
            {
                throw new Exception("Cannot find renderer factory to destroy renderer.");
            }

            mRendererFactories[renderer.Type].destroyInstance(renderer);
        }
    }

    public class ParticleSystemFactory ////////TODO: : MovableObject2DFactory
    {
        public static readonly string FACTORY_TYPE_NAME = "ParticleSystem";

        ////public override MovableObject2D createInstanceImpl(string name, params object[] parameters)
        ////{
        ////    ////////TODO: parameter handling

        ////    int quota = 500;
        ////    string resourceGroup = "AutoDetect";

        ////    if (parameters != null)
        ////    {
        ////        if (parameters[0].GetType() == typeof(int)) quota = (int)parameters[0];
        ////    }

        ////    return ParticleSystemManager.Instance.createSystemImpl(name, quota, resourceGroup);
        ////}

        ////public override void destroyInstance(MovableObject2D obj)
        ////{
        ////    ////////TODO: implement
        ////}
    }
#endif
}
